import React from 'react';
import { Button, message, Upload } from 'antd';
import BaseComponent from '@/components/BaseComponent';
export default class index extends BaseComponent {
  constructor(props) {
    super(props);
    this.state = {
      fileList: [],
    };
  }
  handleOnUploadChange = info => {
    if (info.file.status !== 'uploading') {
      this.setState(
        {
          fileList: info.fileList,
          // uploadLoading: true,
        },
        () => {}
      );
    }
    if (info.file.status === 'done') {
      if (this.hide) {
        this.hide();
      }
      this.setState({
        uploadLoading: false,
      });
      this.successMessage('上传成功,请等待后台处理');
    } else if (info.file.status === 'error') {
      this.errorMessage('文件上传出错,请联系管理员');
    }
  };
  handleUploadBefore = (file, fileList) => {
    this.setState({ uploadLoading: true }, () => {
      this.hide = this.loadingMessage('正在上传...');
    });
    return true;
  };
  render() {
    const { url, currentUser, btnText } = this.props;
    const props = {
      name: 'file',
      multiple: true,
      action: `${url}?user=${currentUser.id}`,
      accept: '.xlsx,.xls',
      headers: {
        authorization: 'authorization-text',
      },
      showUploadList: false,
      onChange: this.handleOnUploadChange,
      beforeUpload: this.handleBeforeUpload,
    };
    return (
      <Upload {...props}>
        <Button type="primary">{btnText}</Button>
      </Upload>
    );
  }
}
